package www.dashu.com.controller;

import www.dashu.com.api.CommonResult;
import www.dashu.com.service.RedisService;
import www.dashu.com.domain.CProductAttributeCategory;
import www.dashu.com.dto.CProductAttributeCategoryQuery;
import www.dashu.com.service.CProductAttributeCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Field;
import java.util.Date;

@Controller
@Api(tags = "CProductAttributeCategoryController", description = "商品属性分类表")
@RequestMapping("/productAttributeCategory")
public class CProductAttributeCategoryController
{

    @Autowired
    CProductAttributeCategoryService productAttributeCategoryService;
    @Autowired
    RedisService redisService;

    @ApiOperation(value = "商品属性分类表列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<CProductAttributeCategory>> list(CProductAttributeCategoryQuery query) {

        try {
            Page<CProductAttributeCategory> pageData = productAttributeCategoryService.findAll(query);
            return CommonResult.success(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @ApiOperation(value = "新增商品属性分类表")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody CProductAttributeCategory productAttributeCategory) {
        productAttributeCategory.setId(productAttributeCategoryService.createId(productAttributeCategory));

        Field[] fields = productAttributeCategory.getClass().getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            if(field.getName().toLowerCase().contains("time")){
                try {
                    field.set(productAttributeCategory, new Date());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        productAttributeCategoryService.save(productAttributeCategory);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改商品属性分类表")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody CProductAttributeCategory productAttributeCategory) {

        Field[] fields = productAttributeCategory.getClass().getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            if(field.getName().toLowerCase().contains("updatetime")){
                try {
                    field.set(productAttributeCategory, new Date());
                 } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        productAttributeCategoryService.save(productAttributeCategory);
        return CommonResult.success();
    }

    @ApiOperation(value = "删除商品属性分类表")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult delete(@RequestBody CProductAttributeCategory productAttributeCategory) {
        productAttributeCategoryService.deleteById(productAttributeCategory.getId());
        return CommonResult.success();
    }

}
